package org.pool.util;

import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import com.zaxxer.hikari.util.PropertyElf;
import org.common.util.JDBCUtil;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;
import java.util.Properties;

/**
 * Hikari连接池的工具类
 *
 * @author yexi
 */
public class HikariUtil {
    private HikariUtil() {
    }

    private static DataSource dataSource = null;

    /**
     * 需要按规范写属性
     * @see PropertyElf#setTargetFromProperties(Object, Properties)
     * @see PropertyElf#setProperty(Object, String, Object, List)
     */
    static {
        try {
            Properties properties = JDBCUtil.getPropertiesConnPool(true);
            HikariConfig hikariConfig = new HikariConfig(properties);
            dataSource = new HikariDataSource(hikariConfig);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static Connection getConnection() {
        try {
            return dataSource.getConnection();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
}
